var statusLists = [
    {
        id: '0',
    text: '全部订单'
    },{
    id: '1',
    text: '待付款'
}, {
    id: '5',
    text: '待发货'
}, {
    id: '2',
    text: '待收货'
}, {
    id: '3',
    text: '已完成'
}, {
    id: '4',
    text: '已取消'
}

];

var ns, toast;
var app = new Vue({
    el: '#app',
    data: {
        id:0,
        orderlist: [],
        statusList: this.statusLists,
        getdata_timeout: null,
        statuscount:{},
        orderCount: 0,
        status: '0',
        keywords:'',
        pageindex: 0,
        pagesize: 10,
        is_searching:false,
        can_scroll:false,
        current_item_length: 0,
        startTime:'',
        endTime:'',
        total: {
            ProcessCount: 0,
        },
        activeDate:0,
        showSelect:false,
        show:false,
        sendMsg:{//发货商品信息
            showsend:false,//显示发货
            ID:0,
            AllQuantity:0,
            AllWeight:0,
            AddressDetailInfo:'',
        },
        outTime:false,//是否超时
    },
    methods: {
        get_List: function() {
            var that = this;
            that.is_searching = true;
            that.can_scroll = false;
            var opt = {
                action: 'getorderlist',
                status: that.status,
                startRowIndex: that.pageindex,
                keywords:that.keywords,
                pageSize: that.pagesize,
                startTime:that.startTime,
                endTime:that.endTime,
            };
            ns.post(opt, function(succeed, data, err) {
                // console.log(JSON.stringify(data.data.List));
                that.is_searching = false;
                if (succeed) {
                    if(data.status){
                        var list=[];
                        if (data.data.List.length == that.pagesize) {
                            that.can_scroll = true;
                        }
                        if (that.pageindex == 0) {
                            list = data.data.List;
                        } else {
                            list = list.concat(data.data.List);
                        }
                        that.current_item_length = list.length;
                        list = list.map(res=>{
                            res.Selected = false
                            return res
                        })
                        that.orderlist=list;
                        console.log(JSON.stringify(that.orderlist));
                        that.statuscount=data.data.Summaty;
                        // that.orderlist = data.data.List;
                    }

                } else if (err) {
                    that.orderlist = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }

            })
        },
        get_timer:function(time){
            var that=this;
            console.log('时间');
            if(time){
                var m1=moment();
                var m2=moment(time);
                var now = m2.diff(m1, 's')*1000;
                if(now>=0){
                    // var ms=moment.utc(now);
                    // nowminute=moment(ms).get('minute');
                    // nowss=moment(ms).get('second');
                    // console.log(nowminute);
                    // console.log(nowss);
                    // console.log(JSON.stringify(nowminute * nowss*1000));
                    // return nowminute * nowss*1000;
                    return now;
                }else{
                    return 0;
                }
            }
            return 0;
        },
        finish:function(item){
            var that=this;
            item.Selected=true;
            var options={};
            options.OrderID=item.ID;
            options.action='checkbusiesscangetorder';
            ns.post(options, function(succeed, data, err) {
                if(succeed){

                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });


        },
        get_data: function(can_scroll) {
            var that = this;
            that.orderCount = "";
            that.total.ProcessCount = "";
            that.get_List();
            //  that.orderCount = that.orderlist.length;
            //  that.total.ProcessCount = that.orderlist.length;
        },
        //导航栏切换
        randomSwitchBtn: function(id) {
            var that = this;
            //调用接口给数据赋值
            that.pageindex=0;
            that.status = id;
            // that.orderCount = "";
            // that.total.ProcessCount = "";
            that.get_List();

        },
        tel_phone: function(item) {
            api.call({
                type: 'tel_prompt',
                number: item.phone
            });
        },
        //订单详情
        open_order_detail: function(id) {
            var that = this;
            ns.openWin('dl_order_detail', '订单详情', {
                id: id,
            });
        },
        //订单关闭
        open_order_Close: function(id) {
            var that = this;

            api.confirm({
                    title: '提示',
                    msg: '确定关闭?',
                    buttons: ['确定', '取消']
                }, function(ret, err) {
                  if (ret.buttonIndex == 1) {
                    //调用关闭接口
                    var opt = {
                        action: 'uporderstatus', //关闭的方法名
                        orderid: id,
                        orderstatus: 4
                    };
                    ns.post(opt, function(succeed, data, err) {
                        if (succeed) {
                            api.toast({
                                msg: '操作成功！',
                                duration: 2000,
                                location: 'bottom'
                            });
                              app.get_data(true);
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }

                    })

                  }


            });


    },
    //接单
    do_accept:function(id){
        var that = this;
        var option={};
            option.OrderID=id;
            option.IsReject=false;
            option.action='businessconfirmorder';
            console.log(JSON.stringify(option));
            ns.post(option, function(succeed, data, err) {
                console.log(JSON.stringify(err));
                console.log(JSON.stringify(data));
                if (succeed) {
                    if(data.status){
                        api.toast({
                            msg: '接单成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        api.sendEvent({
                            name:'shopmsgevent'
                        });
                    }else{
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })
    },
    //拒绝
    do_refuse:function(id){
        var that = this;
        ns.openWin('order_refuse', '拒绝接单', {
            id: id,
        });
    },
    //确认送达
    do_sureSend:function(id){
        var that=this;
        ns.openWin('order_sureSend', '确认送达', {
            id: id,
        });
    },
    //取消订单
    do_removeorder:function(id){
        var that=this;
        that.id=id;
        that.show=true;
    },
    beforeClose:function(action,done){
        var that=this;
        if(action==='confirm'){
            var option={};
            option.OrderID=that.id;
            option.action='cancelorder';
            ns.post(option, function(succeed, data, err) {
                if(succeed){
                    if(data.status){
                        api.toast({
                            msg: '操作成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();
                    }
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
            that.show=false;
            done();
        }else if(action==='cancel'){
            done();
        }
    },
    // 修改费用
    open_editfeiyong:function(id,ShipRateAmount,ShipTimeAmount,PackingFee,AllPrice){
        var that = this;
        ns.openWin('order_detail_edit', '修改费用',{
            OrderID:id,
            ShipRateAmount:ShipRateAmount,
            ShipTimeAmount:ShipTimeAmount,
            PackingFee:PackingFee,
            TotalCost:AllPrice,
        });
    },
    //发货
    open_send:function(ID,AllQuantity,AllWeight,AddressDetailInfo){
        var that=this;
        that.sendMsg.showsend=true;
        that.sendMsg.ID=ID;
        that.sendMsg.AllQuantity=AllQuantity;
        that.sendMsg.AllWeight=AllWeight;
        that.sendMsg.AddressDetailInfo=AddressDetailInfo;
    },
    beforeClose_send:function(action, done){
        var that=this;
        if(action==='confirm'){
            var option={};
            option.OrderID=that.sendMsg.ID;
            option.action='ordersend';
            ns.post(option, function(succeed, data, err) {
                if(succeed){
                    if(data.status){
                        api.toast({
                            msg: '发货成功',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.get_data();
                    }else{
                        api.toast({
                            msg: data.Msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
            that.sendMsg.showsend=false;
            done();
        }else if(action==='cancel'){
            done();
        }
    },
    // 时间筛选
    doClickDay:function(type){
        var that = this;
        that.activeDate=type;
            var startDate = '',
                endDate = '';
            if (type == 1) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate = dateRangeUtil.getDateString(currentDate);
                endDate = startDate;
            }
            if (type == 2) {
                var yesterday = new Date(new Date().setDate(new Date().getDate() - 1));
                startDate = dateRangeUtil.getDateString(yesterday);
                endDate = startDate;
            }
            if (type == 3) {
                startDate = moment().subtract(6,'days').format('YYYY-MM-DD');
                endDate =  moment().format('YYYY-MM-DD');
            }
            that.startTime = startDate;
            that.endTime = endDate;
            that.showSelect=false;
            that.get_data();
    },

    set_status_bar: function () {
        var that = this;
        api.setStatusBarStyle({
            style: 'dark',
            color: 'transparent'
        });
    }
}
});

apiready = function() {
    api.parseTapmode();
    app.set_status_bar();
    ns = window.Foresight.Util;
    ns.init_pull();
    app.get_data(true);


    api.addEventListener({
        name: 'onlogin'
    }, function(ret, err) {
        app.get_data();
    });
    api.addEventListener({
        name: 'shopmsgevent'
    }, function(ret, err){
          app.get_data();
    });
    //搜索
    api.addEventListener({
        name: 'do_open_search'
    }, function(ret, err) {
        ns.openWin('search_frm', '搜索',{
            currentType:'order'
        });
    });
    //筛选
    api.addEventListener({
        name: 'do_open_saixuan'
    }, function(ret, err) {
        app.showSelect=true;
    });

}
